const func = {
    /**
     * 获取树形菜单
     * @method generateMenuTreeData
     * @param  {[type]}             pId   [父ID，传空获取完整的属性结构，也可以传菜单ID获取其子集]
     * @param  {[type]}             array [完整的树列表数据]
     * @return {[type]}                   [description]
     * @author JiangPing
     * @date   2021-08-11
     */
    generateMenuTreeData: function(pId, array) {
        let tree = []
        for (let i = 0; i < array.length; i++) {
            // console.log(array[i].pId + "##########" + pId);
            if (array[i].pId == pId) {
                let json = {
                    id: array[i].id,
                    label: array[i].name,
                    icon: array[i].icon,
                    url: array[i].url
                }
                let children = func.generateMenuTreeData(array[i].id, array);
                if (children.length != 0) {
                    json['children'] = children;
                }
                tree.push(json)
            }
        }
        return tree;
    },

    generateDictionaryTreeData: function(pId, pName, array) {
        let tree = []
        for (let i = 0; i < array.length; i++) {
            if (array[i].pId == pId) {
                let json = {
                    id: array[i].id,
                    pId: pId,
                    pName: pName,
                    code: array[i].code,
                    label: array[i].name
                }
                let children = func.generateDictionaryTreeData(array[i].id, array[i].name, array);
                if (children.length != 0) {
                    json['children'] = children;
                }
                tree.push(json)
            }
        }
        return tree;
    },

    generateDepartmentTreeData: function(pId, array) {
        let tree = []
        for (let i = 0; i < array.length; i++) {
            if (array[i].pId == pId) {
                let json = {
                    id: array[i].id,
                    code: array[i].code,
                    oaCode: array[i].oaCode,
                    label: array[i].name,
                }
                let children = func.generateDepartmentTreeData(array[i].id, array);
                if (children.length != 0) {
                    json['children'] = children;
                }
                tree.push(json)
            }
        }
        return tree;
    },

    generatePermissionTreeData: function(pId, array) {
        let tree = []
        for (let i = 0; i < array.length; i++) {
            if (array[i].pId == pId) {
                let json = {
                    id: array[i].id,
                    label: array[i].name,
                    category: 'menu'
                }
                let permissions = array[i].permissions;
                if (permissions.length > 0) {
                    let children = [];
                    for (let k = 0; k < permissions.length; k++) {
                        let item = {
                            id: permissions[k].id,
                            label: permissions[k].name,
                            category: 'operation'
                        }
                        children.push(item);
                    }
                    json['children'] = children;

                }
                let children = func.generatePermissionTreeData(array[i].id, array);
                if (children.length != 0) {
                    json['children'] = children;
                }
                tree.push(json)
            }
        }
        return tree;
    }
}

export default func;